from django.contrib.auth.models import User
from rest_framework import viewsets

from bugs.serializers import UserSerializer, BugsSerializer, DictionarySerializer
from bugs.models import Bugs, Dictionary
from rest_framework import filters
from rest_framework.pagination import PageNumberPagination


# Create your views here.

# ViewSets define the view behavior.
class StandardResultsSetPagination(PageNumberPagination):
    """
    配置分页规则
    """
    page_size = 10
    page_query_param = 'page'


class UserViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer


class BugsViewSet(viewsets.ModelViewSet):
    queryset = Bugs.objects.all()
    serializer_class = BugsSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = (filters.SearchFilter,)
    search_fields = ('type',)


class DictionaryViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Dictionary.objects.all()
    serializer_class = DictionarySerializer
